#!/bin/bash

if [ `uname` == "Darwin" ]; then
  platform="mac"
  gogo_exe="bin/${platform}/gogo"
  slag_exe="bin/${platform}/slag"
  gslag_exe="bin/${platform}/gslag"
  slagc_exe="bin/${platform}/slagc"
  slag_libs="bin/mac/libraries"
  config_filename="~/.profile"

  if [ "$1" == "rebuild" ]; then
    rm -r bin/mac
  fi

  if [ ! -e $gogo_exe ]; then
    echo -------------------------------------------------------------------------------
    echo "> COMPILING GOGO BUILD SYSTEM..."
    echo 
    pushd platforms/mac
    xcodebuild -target gogo
    popd
    mkdir -p bin/mac
    cp platforms/mac/build/Release/gogo $gogo_exe
    chmod a+rx $gogo_exe
  fi

  if [ ! -e $slag_exe ]; then
    echo -------------------------------------------------------------------------------
    echo "> COMPILING SLAG VIRTUAL MACHINE..."
    echo 
    pushd platforms/mac
    xcodebuild -target slag
    popd
    cp platforms/mac/build/Release/slag $slag_exe
    chmod a+rx $slag_exe
  fi

  if [ ! -e $gslag_exe ]; then
    echo -------------------------------------------------------------------------------
    echo "> COMPILING GSLAG VM WITH IMAGE MANIPULATION..."
    echo 
    pushd platforms/mac
    xcodebuild -target gslag
    popd
    cp platforms/mac/build/Release/gslag $gslag_exe
    chmod a+rx $gslag_exe
  fi

  if [ ! -e $slagc_exe ]; then
    echo -------------------------------------------------------------------------------
    echo "> COMPILING SLAG COMPILER... (this may take a while)"
    echo 
    mkdir -p build
    $slag_exe libraries/slag/slagc/slagc.etc slagc.slag -src_path "libraries/slag/standard;libraries/slag/slagc" -dest_path build -cpp
    pushd platforms/mac
    xcodebuild -target slagc
    popd
    cp platforms/mac/build/Release/slagc $slagc_exe
    chmod a+rx $slagc_exe
  fi

else
  if [ "`uname`" == "Linux" ]; then
    platform="linux"
  else
    platform="unix"
  fi
  slag_exe="bin/${platform}/slag"
  gslag_exe="bin/${platform}/gslag"
  slagc_exe="bin/${platform}/slagc"
  gogo_exe="bin/${platform}/gogo"
  slag_libs="bin/${platform}/libraries"
  config_filename="~/.bashrc"

  if [ "$1" == "rebuild" ]; then
    rm -r bin/${platform}
  fi

  if [ ! -e $gogo_exe ]; then
    pushd platforms/${platform}
    echo -------------------------------------------------------------------------------
    echo "> COMPILING GOGO BUILD SYSTEM..."
    echo 
    make gogo
    popd
  fi

  if [ ! -e $slag_exe ]; then
    pushd platforms/${platform}
    echo -------------------------------------------------------------------------------
    echo "> COMPILING SLAG VIRTUAL MACHINE..."
    echo 
    make slag
    popd
  fi

  if [ ! -e $gslag_exe ]; then
    pushd platforms/${platform}
    echo -------------------------------------------------------------------------------
    echo "> COMPILING GSLAG VM WITH IMAGE MANIPULATION..."
    echo 
    make gslag
    popd
  fi

  if [ ! -e $slagc_exe ]; then
    pushd platforms/${platform}
    echo -------------------------------------------------------------------------------
    echo "> COMPILING SLAG COMPILER... (this may take a while)"
    echo 
    make slagc
    popd
  fi
fi

if [ ! -e $slag_libs ]; then
  echo -------------------------------------------------------------------------------
  echo "> COPYING SLAG LIBRARIES..."
  echo "$gogo_exe -bin \"bin/$platform\" all -src_path \"libraries/slag;libraries/slag/standard\""
  $gogo_exe -bin "bin/$platform" libs -src_path "libraries/slag;libraries/slag/standard"
fi

if [ "`which slagc`" == "" ]; then
  echo "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*"
  echo "ATTENTION"
  echo ""
  echo "Edit $config_filename and add the following line at the bottom:"
  echo ""
  echo "  PATH=\"`pwd`/bin/$platform:\$PATH\""
  echo ""
  echo "Then type \"source $config_filename\" or open a new terminal."
  echo "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*"
else
  if [ "$1" == "rebuild" ]; then
    exit
  fi
  $gogo_exe $@
fi

